#include "colorscale.h"
#include "math.h"

ColorScale::ColorScale()
{
    _minValue = 1;
    _maxValue = 1;
    _blueCoefficient = 1;
    _greenCoefficient = 1;
    _orangeCoefficient = 1;
}

QSharedPointer<ColorScale> ColorScale::_instance;

QSharedPointer<ColorScale> ColorScale::instance()
{
    if(_instance.isNull())
    {
        _instance = QSharedPointer<ColorScale>(new ColorScale);
    }
    return _instance;
}

QColor ColorScale::getColor(double value)
{
    if(value < _minValue)
        value = _minValue;
    else if(value > _maxValue)
        value = _maxValue;
    value = (value - _minValue)/(_maxValue - _minValue);

    if(value < _blueCoefficient)
    {
        return QColor(50, (int)(50 + 165*value/_blueCoefficient), 255);
    }
    else if(value < _greenCoefficient)
    {
        return QColor(50, (int)(215 + 40*(value-_blueCoefficient)/(_greenCoefficient-_blueCoefficient)), (int)(255 - 255*(value-_blueCoefficient)/(_greenCoefficient-_blueCoefficient)));
    }
    else if(value < _orangeCoefficient)
    {
        return QColor((int)(50 + 205*(value-_greenCoefficient)/(_orangeCoefficient-_greenCoefficient)), 255, 0);
    }
    else if(value <= 1)
    {
        return QColor(255, (int)(255 - 255*(value-_orangeCoefficient)/(1-_orangeCoefficient)), 0);
    }
    return QColor(255, 255, 255, 0);
}

void ColorScale::setBoundValues(double minValue, double maxValue)
{    
    _minValue = minValue;
    _maxValue = maxValue;

    _blueCoefficient = 0.2;
    _greenCoefficient = 0.5;
    _orangeCoefficient = 0.8;
}

double ColorScale::getMinValue()
{
    return _minValue;
}

double ColorScale::getMaxValue()
{
    return _maxValue;
}
